package com.ten_4_4.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.ten_4_4.hiring.comm.HiringConstants;
import com.ten_4_4.hiring.comm.PageModel;
import com.ten_4_4.hiring.comm.util.AppUtil;
import com.ten_4_4.hiring.company.dao.ApplicationDao;
import com.ten_4_4.hiring.company.dao.PositionDao;
import com.ten_4_4.hiring.company.po.ApplicationPo;
import com.ten_4_4.hiring.company.po.PositionPo;
import com.ten_4_4.hiring.talent.dao.ResumeDao;
import com.ten_4_4.hiring.talent.po.ResumePo;


/**
 * Application Servlet
 *
 * @author Zzy.
 * @since 2016年10月14日
 */
public class ApplicationServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private ApplicationDao applicationDao = new ApplicationDao();
	private ResumeDao resumeDao = new ResumeDao();
	private PositionDao positionDao = new PositionDao();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("delete".equals(command)) {
			delete(request, response);
		} else if ("apply".equals(command)) {
			apply(request, response);
		} else if ("listTalentApplyByPage".equals(command)) {
			listTalentApplyByPage(request, response);
		} else if ("listCompanyVerifyByPage".equals(command)) {
			listCompanyVerifyByPage(request, response);
		} else if ("verify".equals(command)) {
			verify(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/**
	 * 删除某个求职者的求职信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、调用前台传来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2、调用业务逻辑
		int row = applicationDao.delete(id);
		// 3、转向
		response.sendRedirect("TalentServlet?command=main&row=" + row);
	}

	/**
	 * 求职者的简历投递
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void apply(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、调用前台传来的参数
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		// 2、调用业务逻辑
		int talentId = AppUtil.getTalentId(request, response);
		int row = applicationDao.add(talentId, resumeId, positionId);
		// 3、转向
		if (row == 1) {
			request.setAttribute("success", "求职者的简历投递成功~");
		} else {
			request.setAttribute("error", "求职者的简历投递失败~");
		}
		// 回显信息
		response.sendRedirect("PositionServlet?command=detail&id=" + positionId);
	}

	/**
	 * 求职者申请信息的分页查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listTalentApplyByPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int pageNo = HiringConstants.DEFAULT_PAGE_NO;
		// 非登录操作
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		int talentId = AppUtil.getTalentId(request, response);
		// 申请信息
		PageModel<ApplicationPo> pageModel = applicationDao
				.listTalentApplyByPage(talentId, pageNo,
						HiringConstants.DEFAULT_PAGE_SIZE);
		request.setAttribute("pageModel", pageModel);
		// 简历信息
		List<ResumePo> resumeList = resumeDao.list(talentId);
		request.setAttribute("resumeList", resumeList);
		// 转发 -> 优点：可以携带参数，缺点：地址上显示此次Servlet的请求地址，用户可以重复提交！
		request.getRequestDispatcher("view/talent/main.jsp").forward(request,
				response);
	}

	/**
	 * 分页查询某个公司所有的职位申请信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listCompanyVerifyByPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 把查询条件装到ApplicationPo里面
		int companyId = AppUtil.getCompanyId(request, response);
		ApplicationPo application = new ApplicationPo();
		application.setCompanyId(companyId);
		// 表单查询条件
		String positionName = request.getParameter("positionName");
		if (StringUtils.isNotBlank(positionName)) {
			application.setPosition$name(positionName);
		}
		String statusStr = request.getParameter("status");
		if (StringUtils.isNotBlank(statusStr)) {
			int status = Integer.parseInt(statusStr);
			application.setStatus(status);
		}
		
		String appTimeStart = request.getParameter("appTimeStart");
		String appTimeEnd = request.getParameter("appTimeEnd");
		
		application.setAppTimeStart(appTimeStart);
		application.setAppTimeEnd(appTimeEnd);

		int pageNo = HiringConstants.DEFAULT_PAGE_NO;
		// 非登录操作
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}

		// 申请信息
		PageModel<ApplicationPo> pageModel = applicationDao
				.listCompanyVerifyByPage(application, pageNo,
						HiringConstants.DEFAULT_PAGE_SIZE);
		request.setAttribute("pageModel", pageModel);
		List<PositionPo> positionList = positionDao.listById(companyId);
		request.setAttribute("positionList", positionList);
		request.setAttribute("applicationCondition", application);
		request.getRequestDispatcher("WEB-INF/view/company/company-homepage.jsp").forward(request,
				response);
	}

	/**
	 * 企业审核求职者的申请
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void verify(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int status = Integer.parseInt(request.getParameter("status"));
		int applicationId = Integer.parseInt(request
				.getParameter("applicationId"));
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));

		@SuppressWarnings("unused")
		int row = applicationDao.verify(status, applicationId);

		// 转向
		response.sendRedirect("ApplicationServlet?command=listCompanyVerifyByPage&pageNo="
				+ pageNo);
	}
}
